﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 车型服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class testdbService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<testdb> _rep;
    public testdbService(SqlSugarRepository<testdb> rep)
    {
        _rep = rep;
    }
 

    /// <summary>
    /// 分页查询车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<testdbOutput>> Page(testdbInput input)
    {
        var query= _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.title), u => u.title.Contains(input.title.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.desc), u => u.desc.Contains(input.desc.Trim()))

                    .Select<testdbOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddtestdbInput input)
    {
        var entity = input.Adapt<testdb>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeletetestdbInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdatetestdbInput input)
    {
        var entity = input.Adapt<testdb>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<testdb> Get([FromQuery] QueryByIdtestdbInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取车型列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<testdbOutput>> List([FromQuery] testdbInput input)
    {
        return await _rep.AsQueryable().Select<testdbOutput>().ToListAsync();
    }





}

